home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS14.ADF / Tool / select.c < prev    next >
C/C++ Source or Header  |  1989-01-28  |  4KB  |  152 lines

  1. /*
  2.       SELECT.C  Copyright Daniel D. Kary  1986
  3.       Standard Listing
  4. */
  5.  
  6. #include <libraries/dos.h>
  7. #include <stdio.h>
  8.  
  9. FILE *fout;
  10.  
  11. main(argc, argv)
  12. int argc;
  13. char *argv[];
  14. {
  15.    int i;
  16.  
  17.    if(argc < 3){
  18.       printf("USAGE: %s infile(s) outfile\n", argv[0]);
  19.       exit(1);
  20.    }
  21.    argc--;
  22.    if((fout = fopen(argv[argc], "w")) == NULL){
  23.       printf("Unable to write to %s\n", argv[argc]);
  24.       exit(1);
  25.    }
  26.    for(i = 1; i < argc; i++)
  27.       search(argv[i]);
  28. }
  29.  
  30. search(name)
  31. char *name;
  32. /* recursively search "name" for files */
  33. {
  34.    struct FileInfoBlock *FIB, *AllocMem();
  35.    int lock;
  36.    char *subdir;
  37.  
  38.    if((FIB= AllocMem(sizeof(struct FileInfoBlock),0))==0){
  39.       printf("Can not allocate memory for FIB\n");
  40.       return(0);
  41.    }
  42.    if((lock = Lock(name, SHARED_LOCK)) == 0){
  43.       printf("Can not Lock %s\n", name);
  44.       FreeMem(FIB, sizeof(struct FileInfoBlock));
  45.       return(0);
  46.    }
  47.    if((Examine(lock, FIB)) == 0){
  48.       printf("Can not Examine %s\n", name);
  49.       UnLock(lock);
  50.       FreeMem(FIB, sizeof(struct FileInfoBlock));
  51.       return(0);
  52.    }
  53.    if(FIB->fib_DirEntryType > 0){
  54.       printf("%s (dir)\n", name);
  55.       while(ExNext(lock, FIB)){
  56.          subdir = (char *) malloc(strlen(name)+
  57.                   strlen(&FIB->fib_FileName[0])+2);
  58.          strcpy(subdir, name);
  59.          if(not_device(name))
  60.             strcat(subdir, "/");
  61.          strcat(subdir, &FIB->fib_FileName[0]);
  62.          if(FIB->fib_DirEntryType > 0)
  63.             search(subdir);
  64.          else{
  65.             printf("%s\n", subdir);
  66.             read_file(subdir);
  67.          }
  68.          free(subdir);
  69.       }
  70.    }
  71.    else{
  72.       printf("%s\n", name);
  73.       read_file(name);
  74.    }
  75.    UnLock(lock);
  76.    FreeMem(FIB, sizeof(struct FileInfoBlock));
  77. }
  78.  
  79. not_device(name)
  80. char name[];
  81. /* return TRUE if the last char is not a colon */
  82. {
  83.    int i, j;
  84.  
  85.    for( i = 0; name[i]; i++){
  86.       if(name[i] == ':')
  87.          j = FALSE;
  88.       else
  89.          j = TRUE;
  90.    }
  91.    return(j);
  92. }
  93.  
  94. read_file(name)
  95. char *name;
  96. /* find all the struct definitions in the named file
  97.  * and write them on the users output file
  98.  */
  99. {
  100.    FILE *fin;
  101.    char buf1[256], buf2[256], buf3[256];
  102.    int  Found1, Found2, count, q, i, j;
  103.    
  104.    if((fin = fopen(name, "r")) == NULL){
  105.       printf("Unable to read %s\n", name);
  106.       return(0);
  107.    }
  108.  
  109.    while((fgets(&buf1[0], 256, fin)) != NULL){
  110.       if(strncmp(&buf1[0], "struct", 6) == 0){
  111.          Found1 = Found2 = FALSE;
  112.          if(stcpm(&buf1[0], "{", &q)){
  113.             count = 1;
  114.             Found1 = TRUE;
  115.          }
  116.          else{
  117.             if((fgets(&buf2[0], 256, fin)) != NULL){
  118.                if(stcpm(&buf2[0], "{", &q)){
  119.                   count = 1;
  120.                   Found2 = TRUE;
  121.                }
  122.             }
  123.          }
  124.          if(Found1 || Found2){
  125.             for(i = 6; !isalpha(buf1[i]); i++);
  126.             for(j = 0; isalpha(buf1[i]); i++, j++)
  127.                buf3[j] = buf1[i];
  128.             buf3[j] = '\0';
  129.             fprintf(fout,"!%s\n", &buf3[0]);
  130.             fprintf(fout,"%s\n", name);
  131.             fprintf(fout,"%s",&buf1[0]);
  132.          }
  133.          if(Found2)
  134.             fprintf(fout,"%s",&buf2[0]);
  135.          while(Found1 || Found2){
  136.             if((fgets(&buf1[0], 256, fin)) != NULL)
  137.                fprintf(fout,"%s",&buf1[0]);
  138.             else{
  139.                fclose(fin);
  140.                return(0);
  141.             }
  142.             if(stcpm(&buf1[0], "{", &q))
  143.                count++;
  144.             if(stcpm(&buf1[0], "}", &q))
  145.                if(--count == 0)
  146.                   Found1 = Found2 = FALSE;
  147.          }
  148.       }
  149.    }
  150.    fclose(fin);
  151. }
  152.